<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form>
  <mat-toolbar class="flex flex-row" color="primary">
    <h2 translate>{{ importTitle }}</h2>
    <span class="flex-1"></span>
    <div [tb-help]="'entitiesImport'"></div>
    <button mat-icon-button
            (click)="cancel()"
            [disabled]="isImportData"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div mat-dialog-content>
    <mat-vertical-stepper [linear]="true" [selectedIndex]="selectedIndex" #importStepper>
      <mat-step [stepControl]="selectFileFormGroup">
        <form [formGroup]="selectFileFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.select-file' | translate }}</ng-template>
          <fieldset [disabled]="isLoading$ | async">
            <tb-file-input formControlName="importData"
                           required
                           label="{{importFileLabel | translate}}"
                           [allowedExtensions]="'csv,txt'"
                           [accept]="'.csv,application/csv,text/csv,.txt,text/plain'"
                           dropLabel="{{'import.drop-file-csv-or' | translate}}">
            </tb-file-input>
          </fieldset>
        </form>
        <div class="flex flex-row items-center justify-end gap-5">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="cancel()">{{ 'action.cancel' | translate }}</button>
          <button mat-raised-button
                  [disabled]="(isLoading$ | async) || selectFileFormGroup.invalid || !selectFileFormGroup.dirty"
                  color="primary"
                  (click)="nextStep(2)">{{ 'action.continue' | translate }}</button>
        </div>
      </mat-step>
      <mat-step [stepControl]="importParametersFormGroup">
        <form [formGroup]="importParametersFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.configuration' | translate }}</ng-template>
          <fieldset [disabled]="isLoading$ | async" class="flex flex-col">
            <mat-form-field class="mat-block">
              <mat-label translate>import.csv-delimiter</mat-label>
              <mat-select required formControlName="delim">
                <mat-option *ngFor="let delimiter of delimiters" [value]="delimiter.key">
                  {{ delimiter.value }}
                </mat-option>
              </mat-select>
            </mat-form-field>
            <div class="flex flex-col gap-3">
              <mat-checkbox formControlName="isHeader">
                {{ 'import.csv-first-line-header' | translate }}
              </mat-checkbox>
              <mat-checkbox formControlName="isUpdate">
                {{ 'import.csv-update-data' | translate }}
              </mat-checkbox>
            </div>
          </fieldset>
        </form>
        <div class="flex flex-row flex-wrap items-center justify-between">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="previousStep()">{{ 'action.back' | translate }}</button>
          <div class="flex flex-row flex-wrap gap-5">
            <button mat-button
                    [disabled]="(isLoading$ | async)"
                    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
            <button mat-raised-button
                    [disabled]="(isLoading$ | async)"
                    color="primary"
                    (click)="nextStep(3)">{{ 'action.continue' | translate }}</button>
          </div>
        </div>
      </mat-step>
      <mat-step [stepControl]="columnTypesFormGroup">
        <form [formGroup]="columnTypesFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.column-type' | translate }}</ng-template>
          <tb-table-columns-assignment #columnsAssignmentComponent formControlName="columnsParam" [entityType]="entityType"></tb-table-columns-assignment>
        </form>
        <div class="flex flex-row flex-wrap items-center justify-between">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="previousStep()">{{ 'action.back' | translate }}</button>
          <div class="flex flex-row flex-wrap gap-5">
            <button mat-button
                    [disabled]="(isLoading$ | async)"
                    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
            <button mat-raised-button
                    [disabled]="(isLoading$ | async) || columnTypesFormGroup.invalid || !columnTypesFormGroup.dirty"
                    color="primary"
                    (click)="nextStep(4)">{{ 'action.continue' | translate }}</button>
          </div>
        </div>
      </mat-step>
      <mat-step>
        <ng-template matStepLabel>{{ 'import.stepper-text.creat-entities' | translate }}</ng-template>
        <mat-progress-bar color="warn" class="tb-import-progress" mode="indeterminate">
        </mat-progress-bar>
      </mat-step>
      <mat-step>
        <ng-template matStepLabel>{{ 'action.done' | translate }}</ng-template>
        <div class="flex flex-col">
          <p class="mat-body-2" *ngIf="this.statistical?.created">
            {{ translate.instant('import.message.create-entities', {count: this.statistical.created}) }}
          </p>
          <p class="mat-body-2" *ngIf="this.statistical?.updated">
            {{ translate.instant('import.message.update-entities', {count: this.statistical.updated}) }}
          </p>
          <p class="mat-body-2" style="margin-bottom: 0.8em" *ngIf="this.statistical?.errors">
            {{ translate.instant('import.message.error-entities', {count: this.statistical.errors}) }}
          </p>
          <mat-expansion-panel class="advanced-logs" [expanded]="false"
                               *ngIf="this.statistical?.errorsList?.length"
                               (opened)="initEditor()">
            <mat-expansion-panel-header [collapsedHeight]="'38px'" [expandedHeight]="'38px'">
              <mat-panel-title>
                <div class="tb-small" translate>import.details</div>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <mat-divider></mat-divider>
            <div #failureDetailsEditor class="tb-failure-details"></div>
          </mat-expansion-panel>
        </div>
        <div class="flex flex-row items-center justify-end gap-5">
          <button mat-raised-button
                  [disabled]="(isLoading$ | async)"
                  color="primary"
                  (click)="nextStep(6)">{{ 'action.ok' | translate }}</button>
        </div>
      </mat-step>
    </mat-vertical-stepper>
  </div>
</form>
